package com.qfedu.controller;

import com.qfedu.entity.*;
import com.qfedu.service.OrderService;
import com.qfedu.service.impl.OrderServiceImpl;
import com.qfedu.utils.BaseServlet;
import com.qfedu.utils.Constants;
import com.qfedu.utils.RandomUtils;
import java.util.Date;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;

import java.util.List;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 通过 购物车内容和地址的组合 查看 订单预览
     * @param request
     * @param response
     * @return
     */
    public String preView(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();

        try {
            List<Cart> cartList = orderService.selectCart(uid);
            List<Address> addressList = orderService.selectAddress(uid);
            request.setAttribute("cartList",cartList);
            request.setAttribute("addressList",addressList);
            return Constants.FORWARD + "/order.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

    /**
     * 创建订单
     * @param request
     * @param response
     * @return
     */
    public String create(HttpServletRequest request, HttpServletResponse response){
        String oid = RandomUtils.createOrderId();
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();
        int aid = Integer.valueOf(request.getParameter("aid"));
        String sum = request.getParameter("sum");
        BigDecimal ocount = new BigDecimal(sum);
        int ostate = 1;
        String otime = String.valueOf(new Date());
        Item item = new Item();
        try {
            orderService.addOrder(oid, uid, aid, ocount, otime, ostate);
            List<Cart> cartList = orderService.selectCart(uid);
            for (Cart cart : cartList) {
                orderService.addItem(oid,cart.getPid(),cart.getCcount(),cart.getCnum());
            }
            orderService.deleteCart(uid);
            return Constants.FORWARD + "/order?method=show";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

    /**
     * 根据 用户id 查看 我的订单列表
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();
        try {
            List<Orders> ordersList = orderService.getOrderListByUid(uid);
            request.setAttribute("ordersList",ordersList);
            return Constants.FORWARD + "/orderList.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

    /**
     * 根据订单id 用户id 查看订单详情
     * @param request
     * @param response
     * @return
     */
    public String detail(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        int uid = user.getUid();
        String oid = request.getParameter("oid");
        try {
            Orders orders = orderService.queryOrderAddress(oid);
            List<Item> itemList = orderService.queryItemInfo(oid);
            orders.setItems(itemList);
            List<Orders> ordersList = orderService.getOrderListByUid(uid);
            request.setAttribute("order",orders);
            return Constants.FORWARD + "/orderDetail.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }


}
